# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from main.models import Rest
from order.models import Order


@login_required
def index(request, template_name=None):
    tn = template_name
    mess = {}
    if tn == None :
        u = request.user
        tn = "space/customer/index.html"
        if u.has_perm('pub.service'):
            tn = "space/service/index.html" 
        if u.has_perm('pub.rest'):
            tn = "space/rest/orders.html" 
    if tn == "space/customer/index.html":
        mess = {}
        order_trues = Order.objects.filter(user = request.user,state=True).order_by("-mod_date")[0:5] 
        order_falses = Order.objects.filter(user = request.user,state=False).order_by("-mod_date")[0:5]
        consume_rests = Order.objects.filter(user = request.user,state=True).distinct().order_by("-mod_date")[0:5]
        mess['order_trues'] = order_trues
        mess['order_falses'] = order_falses
        mess['consume_rests'] = consume_rests
    if tn == "space/service/index.html":
        mess = {}
    if tn == "space/rest/orders.html":
        mess = {}
        rest = Rest.objects.filter(user = request.user)
        
        manager = Order.objects
        orders  = manager.get_query_set()
        orders = orders.filter(rest = rest)
        if request.POST.get("sn"):
            orders = orders.filter(sn = request.POST["sn"])
        orders = orders.order_by("-mod_date")
        mess['orders'] = orders
    return render_to_response(tn, mess, context_instance=RequestContext(request))



